/**
 * 客户端测试用例模型
 *
 * Created by snowingsea on 2020/03/13.
 */
const utility = require('./../../phoenix/models/utility');

class TestCase {
  constructor(func, functionId, testId, status, kind, data) {
    this.function = func;
    this.functionId = functionId;
    this.testId = testId;
    this.status = status;
    this.kind = kind;
    this.data = data;
  }

  /**
   * 获取测试用例数据key值
   * @param data
   * @param currentKey
   * @param keys
   * @return {*}
   */
  getFieldKeys(data, currentKey = '', keys = []) {
    if (utility.isObjectType(data)) {
      Object.keys(data).forEach((fieldName) => {
        let key;
        if (currentKey) {
          key = `${currentKey}.${fieldName}`;
        } else {
          key = fieldName;
        }
        if (fieldName === '@value' && utility.isStringType(data[fieldName])) {
          currentKey = key;
          keys.push(currentKey);
        } else {
          this.getFieldKeys(data[fieldName], key, keys);
        }
      });
    } else if (utility.isArrayType(data)) {
      data.forEach((field, i) => {
        let key;
        if (currentKey) {
          key = `${currentKey}[${i}]`;
        } else {
          key = `[${i}]`;
        }
        this.getFieldKeys(data[i], key, keys);
      });
    }
    return keys;
  }
}

/**
 * Export model definition object.
 */
module.exports = TestCase;
